package com.manli.api.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.manli.api.base.exception.MyException;
import com.manli.api.dao.OrderInfoDao;
import com.manli.api.enums.QueueNameEnums;
import com.manli.api.enums.ResultEnums;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 延时任务
 * @author shensg
 */
@Service
public class DelayService {

    Logger logger = LoggerFactory.getLogger(DelayService.class);

    @Autowired
    OrderInfoDao orderInfoDao;

    /**
     * json 字符串
     * @param delayMsg
     */
    public void mainDelaySwitch(String queueName , String delayMsg){
        JSONObject object = JSON.parseObject(delayMsg);
        //订单待支付超时关闭
        if(queueName.equals(QueueNameEnums.Delay_PREPAY.getContent())){
            // 获取总订单编号
            String orderNoMain = (String)object.get("orderNoMain");
            // 获取子订单编号
            String orderNoSub = (String)object.get("orderNoSub");
            orderInfoDao.executeUpdate(
                    "update order_info " +
                        "set " +
                        "status = -1 ," +
                        "cancel_time = now() " +
                        "where order_no_main = ? " +
                        "and order_no_sub = ? " +
                        "and status = 0 " , orderNoMain,orderNoSub);
            return;
        }
        throw new MyException(ResultEnums.NO_FOUND_QUEUE_NAME_JOB);


    }
}
